package org.mindrive.system.web.schedule;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.mindrive.system.annotation.LogOperation;
import org.mindrive.system.collection.schedule.ScheduleEntity;
import org.mindrive.system.core.Result;
import org.mindrive.system.parameter.schedule.ScheduleParameter;
import org.mindrive.system.parameter.schedule.ScheduleSearchParameter;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.service.schedule.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.web.BaseController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (Schedule)表控制层
 *
 * @author maweihong
 * @since 2023-12-11 09:41:41
 */

@Api(tags = {"日程管理"})
@RestController
@RequestMapping("/schedule")
@Slf4j
public class ScheduleController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 分页查询所有数据
     *
     * @param parameter 查询实体
     * @return 所有数据
     */
	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询审核单列表(可分页)")
	@ApiOperation(value = "查询审核单列表(可分页)", notes = "查询审核单列表(可分页)")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@PostMapping(path = "/search")
    public Result search(@RequestBody ScheduleSearchParameter parameter) {
        log.info("查询信息列表开始。");
        Result result = new Result();
		try {
			Map<String, Object> data = scheduleService.search(parameter);
			result = Result.ok(data);
		} catch (Exception ex) {
			log.error("分页查询所有数据失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("分页查询所有数据结束。");
		return result;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    

    /**
     * 新增数据
     *
     * @param parameter 实体对象
     * @return 新增结果
     */
	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_CREATE, event = "日程新增")
	@ApiOperation(value = "日程新增", notes = "返回单个对象")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@PostMapping(path = "/insert")
    public Result insert(@Validated @RequestBody ScheduleParameter parameter, BindingResult bindingResult) {
        Result result = new Result();
		try {
			validParams(bindingResult);
            ScheduleEntity data = scheduleService.create(parameter);
			result = Result.ok(data);
		} catch (Exception ex) {
			log.error("新增失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("新增数据结束。");
		return result;
    }

    /**
     * 修改数据
     *
     * @param parameter 实体对象
     * @return 修改结果
     */
	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "日程修改")
	@ApiOperation(value = "日程修改", notes = "返回修改后结果")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id, @Validated @RequestBody ScheduleParameter parameter,
                         BindingResult bindingResult) {
        Result result = new Result();
		try {
			scheduleService.modify(id, parameter);
			result = Result.ok();
		} catch (Exception ex) {
			log.error("修改失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("修改数据结束。");
		return result;
    }

    /**
     * 删除数据(物理删除)
     *
     * @param ids 主键结合
     * @return 删除结果
     */
	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_DELETE, event = "删除数据")
	@ApiOperation(value = "删除数据(逻辑删除)", notes = "返回删除结果")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@PostMapping(path = "/delete")
    public Result delete(@RequestBody List<String> ids) {
		log.info("删除数据开始。");
		Result result = new Result();
		try {
			scheduleService.delete(ids);
			result = Result.ok();
		} catch (Exception ex) {
			log.error("删除数据失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("删除数据结束。");
		return result;
	}

	/**
	 * @param parameter
	 * @describe 报表信息填报(格式修改)
	 * @author lsy
	 * @DATE 2023/10/30 15:22
	 */
	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_CREATE, event = "日程详细信息填报")
	@ApiOperation(value = "日程详细信息填报", notes = "日程详细信息填报")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@PostMapping(path = "/detail/insertgai")
	public Result detailInsertgai(@Validated @RequestBody List<List<Map<String , Object>>> parameter, BindingResult bindingResult) {
		Result result = new Result();
		try {
			validParams(bindingResult);
			scheduleService.detailInsertgai(parameter);
			result = Result.ok();
		} catch (Exception ex) {
			log.error("新增失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("新增数据结束。");
		return result;
	}

	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询日程表详细内容列表")
	@ApiOperation(value = "查询日程表详细内容列表", notes = "查询日程表详细内容列表")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@GetMapping(path = "/searchDetailList/{id}")
	public Result searchDetailList(@PathVariable(value = "id") String id){
		log.info("查询信息列表开始。");
		Result result = new Result();
		try {
			final List<List<Map<String, Object>>> lists = scheduleService.searchDetailListGai(id);
			result = Result.ok(lists);
		} catch (Exception ex) {
			log.error("分页查询所有数据失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("分页查询所有数据结束。");
		return result;
	}

	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询日程表详细内容列表")
	@ApiOperation(value = "查询最新一条日程表详细内容列表", notes = "查询日程表详细内容列表")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@GetMapping(path = "/searchNewDetailList/{type}")
	public Result searchNewDetailList(@PathVariable(value = "type") String type){
		log.info("查询最新一条日程表详细内容列表开始。");
		Result result = new Result();
		try {
			final List<List<Map<String, Object>>> lists = scheduleService.searchDetailNewListGai(type);
			result = Result.ok(lists);
		} catch (Exception ex) {
			log.error("查询最新一条日程表详细内容列表失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("查询最新一条日程表详细内容列表结束。");
		return result;
	}

	//报表导出
	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_EXPORT, event = "报表导出")
	@ApiOperation(value = "报表导出", notes = "报表导出")
	@ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
	@GetMapping("/expertDerive/{approvalId}")
	public void expertDerive(@PathVariable(value = "approvalId")String approvalId, HttpServletResponse response) throws Exception{
		log.info("报表导出开始。");
		try {
			scheduleService.expertDerive(approvalId , response);
		}catch (Exception ex) {
			log.error("报表导出失败。", ex);
		}
		log.info("报表导出结束。");
	}

}

